package com.fer.hr.nmbp.lab2.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.ibatis.session.SqlSession;

import com.hr.fer.nmbp.lab2.domain.Guest;
import com.hr.fer.nmbp.lab2.domain.GuestExample;
import com.hr.fer.nmbp.lab2.persistence.GuestMapper;

public class SearchGuestsWindow extends JFrame {
	private static final long serialVersionUID = -7285905513849286521L;
	
	private JPanel contentPane;
	private JTextField tbName;
	private JTextField tbSurname;
	private JTextField tbJMBG;
	private JList list;
	private DefaultListModel model;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SearchGuestsWindow frame = new SearchGuestsWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void searchClick(){
		model.clear();
		SqlSession s = MainWindow.sessionFactory.openSession();
		GuestMapper mapper = s.getMapper(GuestMapper.class);
		GuestExample ge = new GuestExample();
		String name = tbName.getText();
		String surname = tbSurname.getText();
		String JMBG = tbJMBG.getText();
		boolean execute = true;
		if(name.equals("") && surname.equals("") && JMBG.equals("")) {
			List<Guest> selectedGuests = mapper.selectByExample(null);
			for(int i=0;i<selectedGuests.size();i++){
				model.add(i, selectedGuests.get(i).toString());
			}
			execute = false;
		} else if(name.equals("") && surname.equals("")){
			ge.or().andJmbgEqualTo(JMBG);
		} else if(name.equals("") && JMBG.equals("")) {
			ge.or().andSurnameEqualTo(surname);
		} else if(surname.equals("") && JMBG.equals("")) {
			ge.or().andNameEqualTo(name);
		} else if(surname.equals("")) {
			ge.or().andNameEqualTo(name).andJmbgEqualTo(JMBG);
		} else if(name.equals("")) {
			ge.or().andSurnameEqualTo(surname).andJmbgEqualTo(JMBG);
		} else if(JMBG.equals("")) {
			ge.or().andNameEqualTo(name).andSurnameEqualTo(surname);
		} else {
			ge.or().andNameEqualTo(name).andSurnameEqualTo(surname).andJmbgEqualTo(JMBG);
		}
		
//		if(!tbName.getText().equals("") && tbSurname.getText().equals("")){
//			ge.or().andNameEqualTo(tbName.getText());
//			execute = true;
//		}
//		if(!tbSurname.getText().equals("")){
//			ge.or().andSurnameEqualTo(tbSurname.getText());
//			execute = true;
//		}
//		if(!tbJMBG.getText().equals("")){
//			ge.or().andJmbgEqualTo(tbJMBG.getText());
//			execute = true;
//		}

		if (execute) {
			List<Guest> selectedGuests = mapper.selectByExample(ge);
			for (int i = 0; i < selectedGuests.size(); i++) {
				model.add(i, selectedGuests.get(i).toString());
			}
		}

//			
////			JOptionPane.showMessageDialog(this, "Niste upisali valjane kljuceve pretrage!");
//		}
		
	}
		
	private void closeClick(){
		this.dispose();
	}
	
	/**
	 * Create the frame.
	 */
	public SearchGuestsWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 210, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblIme = new JLabel("Ime:");
		lblIme.setBounds(10, 41, 46, 14);
		contentPane.add(lblIme);
		
		JLabel lblPrezime = new JLabel("Prezime:");
		lblPrezime.setBounds(10, 66, 60, 14);
		contentPane.add(lblPrezime);
		
		JLabel lblUpisitePodatkeZa = new JLabel("Upisite neke podatke za pretragu:");
		lblUpisitePodatkeZa.setBounds(10, 11, 195, 14);
		contentPane.add(lblUpisitePodatkeZa);
		
		JLabel lblJmbg = new JLabel("JMBG:");
		lblJmbg.setBounds(10, 91, 46, 14);
		contentPane.add(lblJmbg);
		
		tbName = new JTextField();
		tbName.setBounds(76, 38, 86, 20);
		contentPane.add(tbName);
		tbName.setColumns(10);
		
		tbSurname = new JTextField();
		tbSurname.setBounds(76, 63, 86, 20);
		contentPane.add(tbSurname);
		tbSurname.setColumns(10);
		
		tbJMBG = new JTextField();
		tbJMBG.setBounds(76, 88, 86, 20);
		contentPane.add(tbJMBG);
		tbJMBG.setColumns(10);
		
		JButton btSearch = new JButton("Pretrazi");
		btSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchClick();
			}
		});
		btSearch.setBounds(10, 116, 152, 23);
		contentPane.add(btSearch);
		
		model = new DefaultListModel();
		list = new JList(model);
		list.setBounds(10, 150, 152, 100);
		contentPane.add(list);
		
		JButton btExit = new JButton("Izlaz");
		btExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeClick();
			}
		});
		btExit.setBounds(10, 261, 152, 23);
		contentPane.add(btExit);
	}
}
